package messageHandler;

import screen.QuestPetScr;
import network.Cmd_Message;
import network.Message;

public class GameQuestPetMsgHandler extends GameCenterMsgHandler{
	static GameQuestPetMsgHandler m_instance = null;
	
    public static GameQuestPetMsgHandler gI() {
        if (m_instance == null) {
            m_instance = new GameQuestPetMsgHandler();
        }
        return m_instance;
    }
	
	public static void onHandler() {		
		GlobalMsgHandler.gI().miniGameMessageHandler = GameQuestPetMsgHandler.gI();			
	}
	public void onMessage(Message msg) {
		// TODO Auto-generated method stub
		try {
            switch (msg.command) {
            case Cmd_Message.START:
            	short roomID = msg.reader().readByte();
            	short boardID = msg.reader().readByte();
            	short timeHidePet = msg.reader().readByte();
            	short numPet = msg.reader().readByte();
            	QuestPetScr.gI().onStartGame(timeHidePet, numPet);
            	break;
            	
            case Cmd_Message.SUBMIT_DATA:
            	roomID = msg.reader().readByte();
        		boardID = msg.reader().readByte();
        		int i, j;
        		
        		byte[] arrcol = new byte[QuestPetScr.gI().m_iNumPet];
        		byte[] arrrow = new byte[QuestPetScr.gI().m_iNumPet];
        		
        		for(i = 0; i < QuestPetScr.gI().m_iNumPet; i++){
        			arrcol[i] = msg.reader().readByte();
        		}
        		
        		for(i = 0; i < QuestPetScr.gI().m_iNumPet; i++){
        			arrrow[i] = msg.reader().readByte();
        		}
        		
        		//clear old data
        		for(i = 0; i < QuestPetScr.gI().m_iNumPet; i++){
        			for(j = 0; j < QuestPetScr.gI().m_iNumPet; j++){
            			QuestPetScr.gI().m_mydata[i][j] = 0;
            		}
        		}
        		for(i = 0; i < QuestPetScr.gI().m_iNumPet; i++){
        			QuestPetScr.gI().m_mydata[arrrow[i]][arrcol[i]] = 1;
        		}
        		break;
        		
            case Cmd_Message.BEGIN_QUEST:
            	roomID = msg.reader().readByte();
            	boardID = msg.reader().readByte();
            	short interval = msg.reader().readByte();
            	int playerID = msg.reader().readInt();
            	QuestPetScr.gI().onBeginQuest(playerID, interval);
            	break;
            case Cmd_Message.SHOOT:
            	roomID = msg.reader().readByte();
				boardID = msg.reader().readByte();
				int playerShoot = msg.reader().readInt();
				int row = msg.reader().readByte();
				int col = msg.reader().readByte();
				byte result = msg.reader().readByte();
				int nextPlayer = msg.reader().readInt();
				QuestPetScr.gI().onShoot(playerShoot, row, col, result, nextPlayer);
				break;
            case Cmd_Message.END_GAME:
            	roomID = msg.reader().readByte();
            	boardID = msg.reader().readByte();
            	int playerId = msg.reader().readInt();
            	QuestPetScr.gI().onEndGame(playerId);
            	break;
            case Cmd_Message.NEXT_TURN:
            	roomID = msg.reader().readByte();
            	boardID = msg.reader().readByte();
            	playerId = msg.reader().readInt();
            	QuestPetScr.gI().onNextTurn(playerId);
            	break;
            	
            default:
            	super.onMessage(msg);
            	break;
            }
		}catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
	}
}
